package distance;

import org.junit.Test;

/**
 * 这个是有点小问题的，并不起作用。
 */
public class Distance3dTest
{
    public String test3d(String point1, String point2,String point3,String point4)
    {
        //注意这里是包含有xyz坐标系的
        Point3d p1 = new Point3d(point1);
        Point3d p2 = new Point3d(point2);
        Point3d p3 = new Point3d(point3);
        Point3d p4 = new Point3d(point4);

        //求出方向1的方向向量
        Point3d f1 = new Point3d();
        f1.setX(p2.x - p1.x);
        f1.setY(p2.y - p1.y);
        f1.setZ(p2.z - p1.z);

        //求出方向2的方向向量
        Point3d f2 = new Point3d();
        f2.setX(p4.x - p3.x);
        f2.setY(p4.y - p3.y);
        f2.setZ(p4.z - p3.z);


        if (f1.x * f2.y * f2.z == f2.x * f1.y * f1.z)
        {
            return "空间线段平行";
        }
        //法向量的阶乘
        Point3d faJie = faFa(f1, f2);

        //各取一点组成向量
        Point3d point3d = new Point3d();
        point3d.setX(p3.x-p1.x);
        point3d.setY(p3.y-p1.y);
        point3d.setZ(p3.z-p1.z);

        double n = point3d.x * faJie.x + point3d.y * faJie.y + point3d.z * faJie.z;
        if (n == 0)
        {
            return "相交！";
        }

        return "不相交";
    }

    /**
     * 2个法向量的叉乘
     */
    private Point3d faFa(Point3d p1,Point3d p2)
    {
        Point3d p = new Point3d();
        p.setX(p1.y * p2.z - p2.y * p1.z);
        p.setY(-(p1.x * p2.z - p2.x * p1.z));
        p.setZ(p1.x * p2.y - p2.x * p1.y);
        return p;
    }

    @Test
    public void test()
    {
        String s = test3d("0,0,0", "1,1,1", "1,1,2", "1,2,3");
        System.out.println(s+"============");
    }
}
